package com.gzist.elderlyserviceplatform.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.entity.dto.ActivityRegistrationQueryDTO;
import com.gzist.elderlyserviceplatform.entity.dto.ActivityRegistrationReviewDTO;
import com.gzist.elderlyserviceplatform.entity.po.ActivityRegistration;
import com.gzist.elderlyserviceplatform.entity.po.ActivityRegistration;
import com.gzist.elderlyserviceplatform.entity.po.ServiceReservation;
import com.gzist.elderlyserviceplatform.entity.po.User;
import com.gzist.elderlyserviceplatform.entity.vo.ActivityRegistrationVO;
import com.gzist.elderlyserviceplatform.entity.vo.ActivityRegistrationVO;
import com.gzist.elderlyserviceplatform.entity.vo.HomePageInfoCountVO;
import com.gzist.elderlyserviceplatform.entity.vo.HomePageInfoCountVOO;
import com.gzist.elderlyserviceplatform.mapper.ActivityRegistrationMapper;
import com.gzist.elderlyserviceplatform.service.ActivityRegistrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzist.elderlyserviceplatform.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 活动报名表 服务实现类
 */
@Service
@RequiredArgsConstructor
public class ActivityRegistrationServiceImpl extends ServiceImpl<ActivityRegistrationMapper, ActivityRegistration> implements ActivityRegistrationService {

    private final ActivityRegistrationMapper activityRegistrationMapper;
    private final UserService userService;
    @Override
    public Page<ActivityRegistrationVO> queryPage(ActivityRegistrationQueryDTO queryDTO) {
        Page<ActivityRegistrationVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return activityRegistrationMapper.queryPage(page, queryDTO);
    }

    @Override
    public void reviewActivityRegistration(ActivityRegistrationReviewDTO reviewDTO) {
        LambdaUpdateWrapper<ActivityRegistration> wrapper = Wrappers.lambdaUpdate();
        if (StrUtil.isNotBlank(reviewDTO.getId())) {
            wrapper.eq(ActivityRegistration::getId, reviewDTO.getId());
        }
        if (StrUtil.isNotBlank(reviewDTO.getReviewStatus())) {
            wrapper.set(ActivityRegistration::getReviewStatus, reviewDTO.getReviewStatus());
        }
        if (StrUtil.isNotBlank(reviewDTO.getReviewOpinion())) {
            wrapper.set(ActivityRegistration::getReviewOpinion, reviewDTO.getReviewOpinion());
        }
        // update拦截未触发，需要手动更新
        wrapper.set(ActivityRegistration::getUpdateTime, LocalDateTime.now());
        this.update(wrapper);
    }

    @Override
    public List<HomePageInfoCountVOO> countActivityRegistration() {
        List<HomePageInfoCountVOO> finalResult = new ArrayList<>();
        // 查询各活动以及其报名人数集合
        List<HomePageInfoCountVO> queryResult = activityRegistrationMapper.countActivityRegistration();
        // 进行最终结果封装
        queryResult.stream().forEach(item -> {
            HomePageInfoCountVOO homePageInfoCountVOO = new HomePageInfoCountVOO();
            BeanUtil.copyProperties(item, homePageInfoCountVOO);
            homePageInfoCountVOO.setColor(generateRandomColor());
            finalResult.add(homePageInfoCountVOO);
        });
        return finalResult;
    }

    @Override
    public void activityRegistrationService(String activity_id) {
        User user =userService.getById(StpUtil.getLoginIdAsString());
        ActivityRegistration activityRegistration =ActivityRegistration.builder()
                .activityId(activity_id)
                .userId(StpUtil.getLoginIdAsString())
                .userName(user.getName())
                .registrationDate(LocalDateTime.now())
                .reviewStatus("待审核")
                .contact(user.getPhone())
                .build();
        activityRegistrationMapper.insert(activityRegistration);
    }


    /**
     * 生成并返回一个随机的RGB颜色代码
     * 颜色代码以十六进制字符串形式表示，格式为"#RRGGBB"
     */
    private String generateRandomColor() {
        // 生成RGB三个通道的随机值（0-255）
        int red = ThreadLocalRandom.current().nextInt(256);
        int green = ThreadLocalRandom.current().nextInt(256);
        int blue = ThreadLocalRandom.current().nextInt(256);
        // 格式化为6位十六进制字符串，补零并转大写
        return String.format("#%02X%02X%02X", red, green, blue);
    }
}
